<template>
  <div class="goods-tabs">
    <nav>
      <a
        :class="{ active: activeComponentName === 'GoodsDetail' }"
        href="javascript:"
        @click="handleTabLight('GoodsDetail')"
      >
        商品详情
      </a>
      <a
        href="javascript:"
        :class="{ active: activeComponentName === 'GoodsComment' }"
        @click="handleTabLight('GoodsComment')"
      >
        商品评价<span>({{ commentNum }})</span>
      </a>
    </nav>
    <!-- 切换内容的地方 -->
    <component :is="activeComponentName"></component>
  </div>
</template>

<script>
import { ref } from "vue";
import GoodsComment from "./GoodsComment";
import GoodsDetail from "./GoodsDetail";
import { getCommentData } from "../../api/goods";
import { useRoute } from "vue-router";

export default {
  name: "GoodsTab",
  setup() {
    const route = useRoute();
    // 当前高亮的是哪个标签
    let activeComponentName = ref("GoodsComment");
    // 评论数量
    let commentNum = ref("");

    getCommentData(route.params.id).then((res) => {
      commentNum.value = res.result.evaluateCount;
    });

    // 点击当前标签高亮
    const handleTabLight = (value) => {
      activeComponentName.value = value;
    };

    return {
      activeComponentName,
      handleTabLight,
      commentNum,
    };
  },
  components: {
    GoodsComment,
    GoodsDetail,
  },
};
</script>

<style lang="less" scoped>
.goods-tabs {
  min-height: 600px;
  background: #fff;

  nav {
    height: 70px;
    line-height: 70px;
    display: flex;
    border-bottom: 1px solid #f5f5f5;

    a {
      padding: 0 40px;
      font-size: 18px;
      position: relative;

      > span {
        color: @priceColor;
        font-size: 16px;
        margin-left: 10px;
      }

      &:first-child {
        border-right: 1px solid #f5f5f5;
      }

      &.active {
        &::before {
          content: "";
          position: absolute;
          left: 40px;
          bottom: -1px;
          width: 72px;
          height: 2px;
          background: @xtxColor;
        }
      }
    }
  }
}
</style>
